import { Effect } from 'dva';
import { Reducer } from 'redux';

import { queryCurrent, updateCurrent, fetchDicData } from '@/services/user';

export interface CurrentUser {
  accountSid?: bigint;
  nickName?: string;
  accountName?:string;
  userSex?: number;
  userHeadImg?: string;
  userMobile?: string;
  userMail?: string;
  userPhone?: string;
  permissions?: any; // 按钮权限集合
}

export interface UserModelState {
  currentUser?: CurrentUser;
}

export interface UserModelType {
  namespace: 'user';
  state: UserModelState;
  effects: {
    fetchCurrent: Effect;
    updateCurrent:any;
    fetchDicData:any;
  };
  reducers: {
    save: Reducer<UserModelState>;
  };
}

const UserModel: UserModelType = {
  namespace: 'user',
  state: {
    currentUser: {}, // 用户信息
    response:{}
  },

  effects: {
    // 查询用户详情
    *fetchCurrent(_, { call, put }) {
      const response = yield call(queryCurrent);
      if(response.statusCode === "0"){
        yield put({
          type: 'save',
          payload: {
            currentUser: response.data
          },
        });
      }
    },
    // 更新用户信息
    *updateCurrent({ payload, callback }, { call, put }) {
      const response = yield call(updateCurrent,payload);
      if(response.statusCode === "0"){
        yield put({
          type: 'save',
          payload: { response },
        });
        callback(response);
      }
    },
  },

  reducers: {
    save(state,action) {
      return {
        ...state,
        ...action.payload,
      };
    }
  },
};

export default UserModel;
